<? defined('SYSPATH') OR die('No direct access allowed.');

if ('localhost'==$_SERVER['HTTP_HOST'])
{
	$root		= '/jbnotes/';
	$scScript	= '';
}
else
{
	$root		= '/';
	global $trackingScript;
	$scScript	= $trackingScript;
}

$media	= $root.'media/';
$images	= $media.'images/';

function menu_item($href,$text,$class='',$level=0)
{
	$divClass = "level$level item";
	$pad = '';
	while($level-->0) $pad .= "&nbsp;";
	return html::div($pad.html::a($text,$href,$class),$divClass);
}

function menu($root,$menu)
{
	$uri  = url::current();
	$res = '';

	$prev = $curr = $next = null;

	foreach ($menu as $group) {
		$items = '';
		foreach ($group as $tag => $ref)
		{
			$is_active = ($uri==$ref);
			$href = $root.$ref;
			$items .= menu_item($href,$tag,$is_active?'active':'');

			if ($is_active) {
				$curr = $tag;
			} elseif ( ! $curr)  {
				$prev = html::a('«&nbsp;'.$tag,$href);
			} elseif ( ! $next) {
				$next = html::a($tag.'&nbsp;»',$href);
			}
		}
		$res .= html::div($items,'menugroup');
	}

	$nav = array();
	if ($prev) array_push($nav,$prev);
	if ($curr) array_push($nav,$curr);
	if ($next) array_push($nav,$next);

	return array($res,html::div(implode(' | ',$nav),'nav'));
}

function nav($nav=null)
{
	$res = array();
	foreach (util::asArray($nav) as $tag=>$ref) {
		array_push($res, html::a($tag,$ref));
	}
	return html::div(implode(' | ',$res),'nav');
}

list($menu,$nav) = menu($root,$menu);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?=$title?></title>
<meta name="description" content="<?=$description?>" />
<meta name="keywords" content="<?=implode(', ',$keywords)?>" />
<meta name="copyright" content="<?=$copyright?>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<? foreach ($sheets as $sheet): ?>
<link rel="stylesheet" href="<?="{$media}css/$sheet.css"?>"
	type="text/css" />
<? endforeach ?>
<script type="text/javascript" src="<?="{$media}js/page.js"?>"></script>
<link rel="shortcut icon" href="<?="{$root}favicon.ico"?>"
	type="image/x-icon" />
</head>
<body>
<div id="body">

<div id="header">
<div class="side1"><a href="<?="$root"?>"><img border="0"
	src="<?="{$root}favicon.png"?>" alt="" /></a></div>
<div class="side2">&nbsp;</div>
<div class="main">&nbsp;<?=$nav?></div>
</div>

<div id="menu" class="side1"><?=$menu?></div>

<div id="content" class="main">
<center>
<div class="infoMsg" id="infoMsg"></div>
<div class="errorMsg" id="errorMsg"></div>
</center>
<?=$content?></div>
<?
	$sess = Session::instance();
	$infoMsg  = html::hQ($sess->get_once('infoMsg'));
	$errorMsg = html::hQ($sess->get_once('errorMsg'));
?>
<script type="text/javascript"><!--
setInfoMsg(<?=$infoMsg?>);
setErrorMsg(<?=$errorMsg?>);
//--></script>

<div id="footer">
<div class="side1">&nbsp;</div>
<div class="side2">&nbsp;</div>
<div class="main"><span class="small">
	<?=$copyright?>
	&#8226; content licensed under
	<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/">
		<img border="0" src="<?="{$images}cc.png"?>" alt="Creative Commons License" /></a>
	&#8226; made with <a href="http://kohanaphp.com/">Kohana</a>, Fabryc,
	and <a href="http://www.php.net/">PHP5</a>
</span></div>
</div>

</div>
<?=$scScript?>
</body>
</html>
